angular.module('send-modal',[])
.service 'SendModal',($ionicModal,$rootScope,$localStorage,$http,$ionicPopup,userData,orderInfo,$ionicHistory,showToast) ->
    $scope = $rootScope.$new()
    $http.get('/tradeapi/',params:{action:'getsendcompany'})
    .success (resp) ->
        if resp.status == '0'
            $scope.sendData = resp.body
            console.log $scope.sendData
        else
            showToast resp.msg
    .error ->
        showToast '您的网络又调皮了'
    $scope.$storage = $localStorage
    $scope.sendInfo = {
        companyName:undefined
        sendNo:undefined
        remark:undefined
    }
    $scope.sendType='0'
    $scope.chooseSendType=(type)->
        $scope.sendType=type
    $scope.doSend=()->
        $scope.hideModal()
    $scope.hideModal=()->
        $scope.modal.hide()
        $scope.modal.remove()
    $scope.getCompanyName = ->
        $scope.ComId = $scope.sendInfo.companyName.ID
    $scope.confirmSend = ->
        requestData={
            userid:userData.getToken()
            action:'changestatus'
            id:orderInfo.order.id
            sendtype:$scope.sendType
            sendcom:''
            sendno:''
            remarks:''
        }
        if $scope.sendType == '0'
            requestData.sendcom=$scope.ComId
            requestData.sendno=$scope.sendInfo.sendNo
            if !requestData.sendcom or !requestData.sendno
                showToast '请填写快递配送信息'
                return
        else
            requestData.remarks=$scope.sendInfo.remark
            if !requestData.remarks
                showToast '请填写商家配送信息'
                return
        $http.post('/utf-8data/',requestData)
        .success (resp) ->
            if resp.status == '0'
                showToast resp.msg
                $scope.modal.hide()
                $scope.modal.remove()
                $scope.sendInfo = {
                    companyName:undefined
                    sendNo:undefined
                    remark:undefined
                }
                _.each $localStorage['orders'],(item)->
                    if (''+item.id==''+orderInfo.order.id)
                        item.orderStatus = '已发货'
                $ionicHistory.goBack()
            else
                showToast resp.msg
        .error ->
            showToast '您的网络又调皮了'
    showModal:() ->
        $ionicModal.fromTemplateUrl('templates/send-modal/send-modal.html',{
            scope:$scope
            animation: 'slide-in-up'
        }).then (modal) ->
            $scope.sendInfo = {
                companyName:undefined
                sendNo:undefined
                remark:undefined
            }
            $scope.modal = modal
            $scope.modal.show()

